﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace ASoft.Web.Base
{
    public static class CurrentContext
    {
        static CurrentContext()
        {
            AUserRepository = new AUserRepository();
            CustomAUserRepository = new CustomAUserRepository();
            CustomRepository = new CustomRepository();
        }

        public static bool IsAdmin
        {
            get
            {
                if (AUser == null)
                {
                    return false;
                }
                return CheckIsAdminGroup(Customs);
            }
        }

        static bool CheckIsAdminGroup(List<Custom> customs)
        {
            return customs.Count(p => p.Code.ToLower() == "admin") > 0;
        }

        public static AUser AUser
        {
            get
            {
                return HttpContext.Current.Session["AUser"] as AUser;

            }
            set
            {
                HttpContext.Current.Session["AUser"] = value;
                HttpContext.Current.Session["Customs"] = null; ;
            }
        }

        public static List<Custom> Customs
        {
            get
            {
                if (AUser == null)
                {
                    return null;
                }
                var _Customs = HttpContext.Current.Session["Customs"] as List<Custom>;
                if (_Customs == null || _Customs.Count == 0)
                {
                    _Customs = CustomAUserRepository
                        .GetEntities(new Dictionary<string, string>() { { "AUserId", AUser.AUserId.ToString() } })
                        .Select(p => p.Custom)
                        .ToList();
                    if (CheckIsAdminGroup(_Customs))
                    {
                        _Customs = CustomRepository
                             .GetEntities(new Dictionary<string, string>() { { "Enable", "1" } });
                    }
                    HttpContext.Current.Session["Customs"] = _Customs;
                }
                return _Customs;
            }
        }

        static string _CustomCode;

        public static string CustomCode
        {
            get
            {
                var _CustomCode = HttpContext.Current.Session["CustomCode"] as string;
                if (string.IsNullOrWhiteSpace(_CustomCode))
                {
                    var custom = Customs.FirstOrDefault();
                    if (custom == null)
                    {
                        _CustomCode = "";
                    }
                    else
                    {
                        _CustomCode = custom.Code;
                    }
                    HttpContext.Current.Session["CustomCode"] = _CustomCode;
                }
                return _CustomCode;
            }
            set
            {
                _CustomCode = string.Empty;
                if (Customs.Count(p => p.Code == value) > 0)
                {
                    _CustomCode = value;
                }
                HttpContext.Current.Session["CustomCode"] = _CustomCode;
            }
        }

        public static bool IsLogin
        {
            get
            {
                var _IsLogin = HttpContext.Current.Session["IsLogin"];
                if (_IsLogin == null)
                    return false;
                return (bool)_IsLogin;
            }
            set
            {
                HttpContext.Current.Session["IsLogin"] = value;
                if (!value)
                {
                    HttpContext.Current.Session.Clear();
                }
            }
        }


        public static AUserRepository AUserRepository { get; set; }

        public static CustomAUserRepository CustomAUserRepository { get; set; }

        public static CustomRepository CustomRepository { get; set; }


        public static bool CheckWapClient()
        {
            bool result = true;
            string clientType = string.Concat(HttpContext.Current.Request.UserAgent);
            if (clientType.ToLower().Contains("mozilla") || clientType.ToLower().Contains("opera"))
            {
                result = false;
            }
            return result;
        }
    }
}
